//
//  Adress.swift
//  AdressBook
//
//  Created by admin on 16/1/21.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit
// 遵循了NSCoding协议
class Address: NSObject,NSCoding {
    var name:NSString!
    var address:NSString!
    // 构造方法
    init(name:NSString,address:NSString) {
        self.name = name
        self.address = address
    }
    // NSCoding协议构造方法
    required init?(coder aDecoder: NSCoder) {
        self.name = aDecoder.decodeObjectForKey("name") as! NSString
        self.address = aDecoder.decodeObjectForKey("address")  as! NSString
    }
    // NSCoding协议解码方法
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(self.name, forKey: "name")
        aCoder.encodeObject(self.name, forKey: "address")
    }
    // 写文件类方法,其实开始我是打算用 NSMutableData的方法来存储的，用NSMutableData的话，在读取的时候会有点麻烦
    class func writeAddresses(addresses:[Address])
    {
        NSKeyedArchiver.archiveRootObject(addresses, toFile: addressesPath)
    }
    // 对文件类方法
    class func readeAddresses()->[Address]
    {
        let mutableData = NSKeyedUnarchiver.unarchiveObjectWithFile(addressesPath)
        if mutableData == nil
        {
            return []
        }
        else
        {
            return mutableData as! [Address]
        }
        
    }
}
